package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author gqs
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
    implements SystemUserService {
  @Autowired private SystemUserMapper systemUserMapper;
  @Autowired private SystemPostService systemPostService;

  @Override
  public IPage<SystemUserItemVo> pageSystemUserItemVo(
      IPage<SystemUserItemVo> page, SystemUserQueryDto queryVo) {
    return systemUserMapper.pageSystemUserItemVo(page, queryVo);
  }

  @Override
  public SystemUserItemVo getSystemUserById(Long id) {
    SystemUser systemUser = this.getById(id);
    // 隐藏密码
    // systemUser.setPassword(null);
    // 查询岗位名称
    SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
    // 封装数据
    SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
    BeanUtils.copyProperties(systemUser, systemUserItemVo);
    systemUserItemVo.setPostName(systemPost.getName());
    return systemUserItemVo;
  }

  @Override
  public boolean saveOrUpdateSystemUser(SystemUser systemUser) {
    // 用户名和手机号都不能重复
    Long id = systemUser.getId() ==null? -1L : systemUser.getId();
    SystemUser dbSystemUser = this.getOne(Wrappers.lambdaQuery(SystemUser.class)
            .eq(SystemUser::getUsername, systemUser.getUsername())
            .or()
            .eq(SystemUser::getPhone, systemUser.getPhone())
            .last("LIMIT 1"));
    if(dbSystemUser!=null && !id.equals(dbSystemUser.getId())){
      throw new StgyException(ResultCodeEnum.SYSTEM_USER_EXISTS);
    }
    // 新增时 设置默认头像 默认状态
    if(systemUser.getId() == null){
      systemUser.setAvatarUrl("https://img0.baidu.com/it/u=499887270,3457043283&fm=253&fmt=auto&app=138&f=PNG?w=500&h=590");
      systemUser.setStatus(BaseStatus.ENABLE);//默认新增的账号 可用
    }
    if (systemUser.getPassword() !=null){
      systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
    }
    return this.saveOrUpdate(systemUser);
  }

  @Override
  /**
   * 检查用户名是否可用
   * 通过查询数据库中是否已存在该用户名来判断用户名是否可用
   *
   * @param username 要检查的用户名
   * @return 如果用户名可用，则返回true；否则返回false
   */
  public boolean isUsernameAvailable(String username) {
      // 查询数据库中用户名为指定值的记录数
      long count = this.count(Wrappers.lambdaQuery(SystemUser.class).eq(SystemUser::getUsername, username));
      // 判断用户名是否可用：如果记录数为0，则表示用户名可用
      return count == 0;
  }


  @Override
  public boolean updateStatusByUserId(Long id, BaseStatus status) {
    boolean flag = this.update(Wrappers.lambdaUpdate(SystemUser.class)
            .eq(SystemUser::getId, id)
            .set(SystemUser::getStatus, status));
    return flag;
  }
}
